using System;
using System.Collections.Generic;
using System.Text;
using Ent.Web.Helper;
namespace Ent.Web.Helper
{
    public class CriteriaHelper
    {
        public Dictionary<string, string> Criteria;
        public CriteriaHelper(string path, string name)
        {
            XMLHelper xml = new XMLHelper(path);
            Criteria = xml.GetNameValuePair("//Vibzworld/Criteria[@name='" + name + "']", "Option", "name", "type");
        }
        public string[] Options
        {
            get
            {
                string[] retValue = new string[Criteria.Count];
                int i = 0;
                foreach (string s in Criteria.Keys)
                {
                    retValue.SetValue(s, i);
                    i++;
                }
                return retValue;
            }
        }
        public string GetJavascriptCase(string name, string checkVariable, string setVariable)
        {
            string type="";
            if (Criteria.ContainsKey(name))
            {
                type = Criteria[name];
                switch (type)
                {
                    case "equal":
                        return "if(" + checkVariable + ".toLowerCase()!='" + name.ToLower() + "') " + setVariable + "=false;";
                    case "greater":
                        if (name.Length >= 1)
                        {
                            string part = name.Substring(0, name.Length - 1);
                            return "if(" + checkVariable + "<=" + part + ") " + setVariable + "=false;";
                        }
                        break;
                    case "smaller":
                        if (name.Length >= 1)
                        {
                            string part = name.Substring(0, name.Length - 1);
                            return "if(" + checkVariable + ">=" + part + ") " + setVariable + "=false;";
                        }
                        break;
                    case "between":
                        string[] parts = name.Split(new char[] { '-' }, 2, StringSplitOptions.RemoveEmptyEntries);
                        if (parts.Length == 2)
                            return "if(" + checkVariable + "<" + parts.GetValue(0).ToString() + " || " + checkVariable + ">" + parts.GetValue(1).ToString() + ") " + setVariable + "=false;";
                        break;
                }
            }
            return "";
        }
    }
}
